package com.mm.base.exception;

import com.mm.util.result.DResult;
import com.mm.util.result.ResultCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * 描述: 自定义全局异常处理类
 *
 * @author: yanglin
 * @Date: 2021-05-18-10:55
 * @Version: 1.0
 */
@Slf4j
@ControllerAdvice
public class GlobalExceptionHandler {

    /**
     * 处理自定义异常
     *
     * @param req
     * @param e
     * @return
     */
    @ExceptionHandler(value = NoteException.class)
    @ResponseBody
    public DResult NoteExceptionHandler(HttpServletRequest req, NoteException e) {
        log.error("发生业务异常！原因是：{}", e.getErrorMsg());
        return DResult.failed(e.getErrorCode(), e.getErrorMsg());
    }


    /**
     * 处理自定义异常
     *
     * @param req
     * @param e
     * @return
     */
    @ExceptionHandler(value = MuMuException.class)
    @ResponseBody
    public DResult NoteExceptionHandler(HttpServletRequest req, MuMuException e) {
        log.error("发生业务异常！原因是：{}", e.getErrorMsg());
        return DResult.failed(ResultCode.MUMU_ERROR.getCode(), e.getErrorMsg());
    }

    /**
     * 处理自定义异常
     *
     * @param req
     * @param e
     * @return
     */
    @ExceptionHandler(value = FileException.class)
    @ResponseBody
    public DResult FileExceptionHandler(HttpServletRequest req, FileException e) {
        // log.error("发生文件操作异常！原因是：{}", e.getErrorMsg());
        return DResult.failed(e.getErrorCode(), e.getErrorMsg());
    }

    /**
     * 处理空指针异常
     *
     * @param req
     * @param e
     * @return
     */
    @ExceptionHandler(value = NullPointerException.class)
    @ResponseBody
    public DResult NullExceptionHandler(HttpServletRequest req, NullPointerException e) {
        log.error("发生空指针异常！原因是：{}", e);
        return DResult.failed(ResultCode.SYSTEM_EXECUTION_ERROR);

    }

    /**
     * 处理其他异常
     *
     * @param req
     * @param e
     * @return
     */
    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public DResult ExceptionHandler(HttpServletRequest req, Exception e) {
        log.error("发生未知异常！原因是：{}", e);
        return DResult.failed(ResultCode.SYSTEM_EXECUTION_ERROR);

    }
}
